/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imessenger;

import java.awt.Toolkit;

import ymsg.network.*;
import ymsg.network.event.*;
import ymsg.support.*;
import ymsg.test.*;
import ymsg.support.MessageDecoder;

/**
 *
 * @author Shelley Jane C. Co
 */
public class IMListener implements SessionListener
{
    private IMClient imClient;
    private IMchatGui chatGui;
    private MessageDecoder messageDecoder;
    
    public IMListener(IMClient imc)
    {
        this.imClient = imc;
        this.messageDecoder = new MessageDecoder();
        this.chatGui = null;
    }
    
    public void openOldChatWindow(Object value)
    {
        IMchatGui chatWindow = (IMchatGui)value;
        chatWindow.setVisible(true);        
    }

    public void fileTransferReceived(SessionFileTransferEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void connectionClosed(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
        this.imClient.logout();
    }

    public void listReceived(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void messageReceived(SessionEvent ev) {
        //throw new UnsupportedOperationException("Not supported yet.");
        System.out.println("Message to : "+ev.getTo());
        System.out.println("Message from : "+ev.getFrom());
        System.out.println("message received");
        
        String message = this.messageDecoder.decodeToText(ev.getMessage());
        
        System.out.println(this.imClient.getHostContacts().contactExists(ev.getFrom()));
        System.out.println(this.chatGui == null);
        
        Toolkit.getDefaultToolkit().beep();
        if (this.chatGui == null || !this.imClient.getHostContacts().contactExists(ev.getFrom()))
        {
            IMchatGui chatWindow = new IMchatGui(this.imClient);
            chatWindow.setVisible(true);
            chatWindow.setTitle(this.imClient.getSessionHost()+" - "+ev.getFrom());
            chatWindow.setReceiver(ev.getFrom());
            chatWindow.getSendTxtArea().setCaretPosition(0);
            
            this.setChatGui(chatWindow);
            this.chatGui.updateChatArea(ev.getFrom() + ": " + message+'\n');
            this.imClient.getHostContacts().getOnContact().put(ev.getFrom(), chatWindow);            
        }
        else
        {
            Object chatWindow = this.imClient.getHostContacts().getOnContact().get(ev.getFrom());
            this.chatGui = (IMchatGui)chatWindow;
            
            if (this.chatGui.isVisible() == false)
                this.chatGui.setVisible(true);
            this.chatGui.updateChatArea(ev.getFrom() + ": " + message+'\n');
        }
	
    }

    public void buzzReceived(SessionEvent ev) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Toolkit.getDefaultToolkit().beep();        
        String message = this.messageDecoder.decodeToText(ev.getMessage());
        
        if (this.chatGui == null || !this.imClient.getHostContacts().contactExists(ev.getFrom()))
        {
            IMchatGui chatWindow = new IMchatGui(this.imClient);
            chatWindow.setVisible(true);
            chatWindow.setTitle(this.imClient.getSessionHost()+" - "+ev.getFrom());
            chatWindow.setReceiver(ev.getFrom());
            chatWindow.getSendTxtArea().setCaretPosition(0);
            
            this.setChatGui(chatWindow);
            this.chatGui.updateChatArea(ev.getFrom() + "BUZZ!!!\n");
            this.imClient.getHostContacts().getOnContact().put(ev.getFrom(), chatWindow);            
        }
        else
        {
            Object chatWindow = this.imClient.getHostContacts().getOnContact().get(ev.getFrom());
            this.chatGui = (IMchatGui)chatWindow;
            this.chatGui.setVisible(true);
            this.chatGui.updateChatArea(ev.getFrom() + "BUZZ!!!\n");
        }
        
    }

    public void offlineMessageReceived(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void errorPacketReceived(SessionErrorEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void inputExceptionThrown(SessionExceptionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void newMailReceived(SessionNewMailEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void notifyReceived(SessionNotifyEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void contactRequestReceived(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void contactRejectionReceived(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void conferenceInviteReceived(SessionConferenceEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void conferenceInviteDeclinedReceived(SessionConferenceEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void conferenceLogonReceived(SessionConferenceEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void conferenceLogoffReceived(SessionConferenceEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void conferenceMessageReceived(SessionConferenceEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void friendsUpdateReceived(SessionFriendEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void friendAddedReceived(SessionFriendEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void friendRemovedReceived(SessionFriendEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void chatLogonReceived(SessionChatEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void chatLogoffReceived(SessionChatEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void chatMessageReceived(SessionChatEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void chatUserUpdateReceived(SessionChatEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void chatConnectionClosed(SessionEvent arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public IMClient getImClient() {
        return imClient;
    }

    public void setImClient(IMClient imClient) {
        this.imClient = imClient;
    }

    public void setChatGui(IMchatGui chatGui) {
        this.chatGui = chatGui;
    }
    
}
